<?php

/**
 * ViewProposedChange function 
 * 
 * Return return all ProposedChangeInformation 
 * 
 * @author Will Zhao <kexinzhao666@gmail.com> 
 */
include_once 'UTerrorcode.php';
include_once 'UTcheckAuth.php';
include_once 'UTDBGlobal.php';

/**
 * @param $userAuthName string 
 * @param $userAuthPassHash string 
 * @return array|null 
 */
function ViewProposedChange($userAuthName, $userAuthPassHash) {
    global $dbaddress;
    global $dbuser;
    global $dbpassword;
    global $dbdatabasename;

    $array = array();
    if (checkAuthentication($userAuthName, $userAuthPassHash)) {
        $conn = mysqli_connect($dbaddress, $dbuser, $dbpassword, $dbdatabasename);
        $sql = "SELECT Account.accName,proposedchange.*,roledate.startTime, roledate.endTime FROM proposedchange LEFT JOIN (roledate, Account) ON roledate.empID = proposedchange.empID AND roledate.roleID = proposedchange.roleID and Account.empID=proposedchange.empID";
        $sql = "SELECT a1.accName as accName, e1.empID as empID, e1.empFname as empFname, e1.empLname as empLname, a2.accName as myAccName, e2.empID as myEmpID, e2.empFname as myEmpFname, e2.empLname as myEmpLname, p.roleID, roleName, p.dateID, datestamp, startTime, endTime FROM account as a1, account as a2, dates as d, employees as e1, employees as e2, proposedchange as p, roles as r, roledate as rd WHERE p.empID = a1.empID AND myEmpID = a2.empID AND d.dateID = p.dateID AND r.roleID = p.roleID AND p.roleID = rd.roleID AND p.empID = rd.empID AND p.dateID = rd.dateID AND p.empID = e1.empID AND p.myEmpID = e2.empID LIMIT 0, 30 ";
        $result = $conn->query($sql);
        $error = ErrorCode::failRead;
        if ($result) {
            if ($result->num_rows > 0) {
                while ($row = $result->fetch_array()) {
                    $array[] = array(
                        'accName' => $row['accName'],
                        'empID' => $row['empID'],
                        'empFname' => $row['empFname'],
                        'empLname' => $row['empLname'],
                        'roleID' => $row['roleID'],
                        'roleName' => $row['roleName'],
                        'dateID' => $row['dateID'],
                        'dateStamp' => $row['datestamp'],
                        'myEmpID' => $row['myEmpID'],
                        'myAccName' => $row['myAccName'],
                        'myEmpFname' => $row['myEmpFname'],
                        'myEmpLname' => $row['myEmpLname'],
                        'startTime' => $row['startTime'],
                        'endTime' => $row['endTime']
                    );
                }
                $error = ErrorCode::successRead;
            }
        } else {
            $error = ErrorCode::failRead;
        }
    } else {
        $error = ErrorCode::authFailRead;
    }
    array_unshift($array, $error);
    $result->free();
    $conn->close();
    return $array;
}
?> 